package com.lbn.jfqsc.app.account;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.lbn.baseframe.base.BaseFragment;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.MainActivity;
import com.lbn.jfqsc.app.account.collection.CollectionActivity;
import com.lbn.jfqsc.app.account.info.InfoActivity;
import com.lbn.jfqsc.app.account.order.OrderActivity;
import com.lbn.jfqsc.app.account.recruitment.RecruitmentActivity;
import com.lbn.jfqsc.app.account.release.ReleaseActivity;
import com.lbn.jfqsc.app.account.resume.ResumeActivity;
import com.lbn.jfqsc.app.login.LoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountFragment extends BaseFragment<AccountPresenter> implements IAccountView {


    @BindView(R.id.info)
    Button info;
    @BindView(R.id.post_recruitment)
    Button postRecruitment;
    @BindView(R.id.m_collection)
    Button mCollection;
    @BindView(R.id.m_help)
    Button mHelp;
    @BindView(R.id.m_order)
    Button mOrder;
    @BindView(R.id.m_resume)
    Button mResume;
    @BindView(R.id.post_organization)
    Button postOrganization;
    @BindView(R.id.login_cover)
    RelativeLayout loginCover;
    @BindView(R.id.clear_user)
    Button clearUser;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_account, null, false);
            ButterKnife.bind(this, rootView);
        }
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        resetUi();
        updateUi();
    }

    public static AccountFragment newInstance() {
        AccountFragment fragment = new AccountFragment();
        return fragment;
    }

    @Override
    public AccountPresenter createPresenter() {
        return new AccountPresenter();
    }

    @OnClick({R.id.login, R.id.login_cover, R.id.clear_user, R.id.info, R.id.post_recruitment, R.id.m_collection, R.id.m_help, R.id.m_order, R.id.m_resume, R.id.post_organization})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.info:
                InfoActivity.startActivity(getContext());
                break;
            case R.id.post_recruitment:
                RecruitmentActivity.startActivity(getContext());
                break;
            case R.id.m_collection:
                CollectionActivity.startActivity(getContext());
                break;
            case R.id.m_help:
                OrderActivity.startActivity(getContext(), "Support");
                break;
            case R.id.m_order:
                OrderActivity.startActivity(getContext(), "Order");
                break;
            case R.id.m_resume:
                ResumeActivity.startActivity(getContext());
                break;
            case R.id.post_organization:
                ReleaseActivity.startActivity(getContext());
                break;
            case R.id.clear_user:
                App.clearUser();
                updateUi();
                ((MainActivity) getActivity()).clearUser();
                break;
            case R.id.login:
                LoginActivity.startActivity(getContext());
                break;
        }
    }


    public void updateUi() {

        if (rootView == null) {
            return;
        }

        if (App.isLogin()) {
            loginCover.setVisibility(View.GONE);
            clearUser.setVisibility(View.VISIBLE);
        } else {
            loginCover.setVisibility(View.VISIBLE);
            clearUser.setVisibility(View.GONE);
        }


        info.setVisibility(View.VISIBLE);
//        mCollection.setVisibility(View.VISIBLE);
        if (App.isMerchant()) {//商家 个人信息、发布招聘、我的收藏、我的赞助
            postRecruitment.setVisibility(View.VISIBLE);
            mHelp.setVisibility(View.VISIBLE);
        } else if (App.isOrganizationUser()) {//社团 个人信息、我的简历、我的收藏、发布社团
            postOrganization.setVisibility(View.VISIBLE);
            mResume.setVisibility(View.VISIBLE);
        } else if (App.isCommonUser()) {//普通用户 个人信息、我的简历、我的收藏、我的订单
            mOrder.setVisibility(View.VISIBLE);
            mResume.setVisibility(View.VISIBLE);
        } else {
            resetUi();
        }
    }

    private void resetUi() {
        info.setVisibility(View.GONE);
        postRecruitment.setVisibility(View.GONE);
        mCollection.setVisibility(View.GONE);
        mHelp.setVisibility(View.GONE);
        mOrder.setVisibility(View.GONE);
        mResume.setVisibility(View.GONE);
        postOrganization.setVisibility(View.GONE);
    }


}
